package com.pym.controller;

import com.pym.common.Pagination;
import com.pym.common.PaginationUtils;
import com.pym.common.ResultMsg;
import com.pym.entity.Wpxx;
import com.pym.enums.ResultEnum;
import com.pym.service.WpglService;
import com.pym.vo.WpxxVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author Jerry
 * @Description //TODO 物品管理控制类
 * @Date 15:34 2021/8/16
 **/
@Api(tags = {"物品管理操作接口"})
@RestController
@RequestMapping("/wpgl")
public class WpglController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    public WpglService wpglService;


    /**
     * 通过主键更新单条数据
     * @param id 主键
     * @param wpxx 修改物品信息实体
     * @return ResultMsg 返回Json
     */
    @ApiOperation(value = "根据主键更新物品信息接口")
    @RequestMapping(value = "/updateWpxxById", method = RequestMethod.POST, produces = "application/json")
    public ResultMsg updateWpxxById(@RequestParam(value = "id") String id, @RequestBody Wpxx wpxx) {
        // 将id放入实体对象中
        wpxx.setId(id);
        // 根据实体对象更新数据
        int result = this.wpglService.updateWpxxById(wpxx);
        logger.info("修改记录成功数：{}",result);
        return result <= 0 ?
                new ResultMsg(ResultEnum.WPXX_UPDATE_ERROR) :
                new ResultMsg(ResultEnum.SUCCESS);
    }

    /***
     * 通过主键删除单条数据（支持批量删除）
     * @param id 主键
     * @return Json字符串
     */
    @ApiOperation(value = "根据主键删除物品信息接口（支持批量删除）")
    @RequestMapping(value = "/deleteWpxxById/", method = RequestMethod.POST, produces = "application/json")
    public ResultMsg deleteWpxxById(@RequestParam(value = "id") String id) {
        // 根据ID删除数据
        int result = this.wpglService.deleteWpxxById(id);
        logger.info("删除记录成功数：{}",result);
        return result <= 0 ?
                new ResultMsg(ResultEnum.WPXX_DELETE_ERROR) :
                new ResultMsg(ResultEnum.SUCCESS);
    }

    /***
     * 查询所有数据(条件查询)
     * @param wpxx 查询条件
     * @param startDate 起始时间
     * @param endDate 结束时间
     * @param indexNo 起始页码
     * @param row 每页显示行数
     * @return ResultMsg 返回Json
     */
    @ApiOperation(value = "查询物品信息数据接口")
    @RequestMapping(value = "/queryAll", method = RequestMethod.POST, produces = "application/json")
    public ResultMsg getAll(@RequestBody Wpxx wpxx,
                            @RequestParam(value = "startDate",required = false) String startDate,
                            @RequestParam(value = "endDate",required = false) String endDate,
                            @RequestParam(value = "row",required = false) Integer row,
                            @RequestParam(value = "indexNo",required = false) Integer indexNo){

        List<Wpxx> list = wpglService.getAll(wpxx, startDate, endDate);
        PaginationUtils paginationUtils = new PaginationUtils();
        // 如有传行数，则设置为传过来的行数
        if(row != null && row > 0){
            paginationUtils.setRows(row);
        }
        paginationUtils.setIndexNo(indexNo);
        paginationUtils.setList(list);
        Pagination pagination = new Pagination(list.size(), paginationUtils.getList(), indexNo, row);
        return new ResultMsg(pagination);
    }

    /**
     * @Author Jerry
     * @Description //TODO 导出物品信息（Excel）
     * @Date 16:37 2021/8/18
     * @Param [response 内置参数, wpxx 查询条件实体, registerDate 查询日期]
     **/
    @ApiOperation(value = "导出物品信息接口")
    @RequestMapping(value = "/exportWpxx", method = RequestMethod.POST)
    public void exportAdministrativeAttend(HttpServletResponse response,
                                           @RequestBody Wpxx wpxx,
                                           @RequestParam(value = "startDate",required = false) String startDate,
                                           @RequestParam(value = "endDate",required = false) String endDate) throws IllegalAccessException, NoSuchFieldException, IOException {
        wpglService.exportWpxx(response, wpxx, startDate, endDate);
    }

    /**
     * @Author Jerry
     * @Description //TODO 根据主键查询物品信息接口
     * @Date 14:44 2021/8/19
     * @Param [id]
     * @return [java.lang.String]
     **/
    @ApiOperation(value = "根据主键查询物品信息接口")
    @RequestMapping(value = "/queryWpxxById/", method = RequestMethod.GET, produces = "application/json")
    public ResultMsg queryWpxxById(@RequestParam(value = "id") String id) {
        WpxxVo wpxxVo = wpglService.queryWpxxById(id);
        return new ResultMsg(wpxxVo);
    }
}
